<?php
namespace Common\Model\Common;

use Common\Base\ModelBase;

class SysLogModel extends ModelBase
{
    /**
     * 得到用户列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getSysLog($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='')
    {
        //查询条件
        $where[] = '1';
        if( ! empty($search))
        {
            isset($search['controller']) && $search['controller']!='' && $where['controller'] = array('like', '%'.$search['controller'].'%');
            isset($search['action']) && $search['action']!='' && $where['action'] = array('like', '%'.$search['action'].'%');
            isset($search['admin_name']) && $search['admin_name']!='' && $where['admin_name'] = array('like', '%'.$search['admin_name'].'%');
        }

        //排序
        $orderBy = 'logid desc';
        $by = trim($by);
        if($by != ''){ $orderBy = $by; }

        //SQL
        $sql['sql']    = $this->md->table(getTable('common_sys_log'))->where($where)->field($fields)->order($orderBy)->limit((($page - 1)*$pageSize), $pageSize)->getSelectSQL();
        $sql['sql_ct'] = $this->md->table(getTable('common_sys_log'))->where($where)->getCountSQL();
        //ddd($sql,1);

        return $this->getData($w, $sql);
    }

    /**
     * 添加系统日志
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addSysLog($dat = array())
    {
        return $this->addRow($dat, getTable('common_sys_log'));
    }

    /**
     * 操作日志删除
     *
     * @param string $where
     *
     * @return mixed
     */
    public function logDelete($where = '')
    {
        return $this->md->table(getTable('common_sys_log'))->where($where)->delete();
    }
}